export const ddLogin =()=>{
  let key = 'dingehqd9t9mtf6ja5e1'
  let url = "http://qianxinoauth.net/login"
  const URL = encodeURIComponent(`https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${key}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${url}`)
  window.DDLogin({
    id: 'login_D',
    style: 'border: none;background-color: rgba(255,255,255,0.01) ',
    goto: URL,
    width: '300px',
    height: '300px',
  })
  
  const handleMessage = async (event) => {
    const origin = event.origin
    const loginTmpCode = event.data
    let gotoURL = encodeURIComponent('http://qianxinoauth.net/login')
    const URLS = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${key}&response_type=code&scope=snsapi_login&state=dinglogin&redirect_uri=${gotoURL}&loginTmpCode=${loginTmpCode}`
    
    if (origin == 'https://login.dingtalk.com') { //判断是否来自ddLogin扫码事件。
      //获取到loginTmpCode后就可以在这里构造跳转链接进行跳转了
      window.open(encodeURI(URLS), '_parent')
    } else {
    }
  }
  if (typeof window.addEventListener != 'undefined') {
    window.addEventListener('message', handleMessage, false)
  } else { 
    if (typeof window.attachEvent != 'undefined') {
      window.attachEvent('onmessage', handleMessage)
    }
  }
}
